package org.niux.aims.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.niux.aims.common.web.AimsBaseAction;
import org.niux.aims.models.EntityType;
import org.niux.aims.models.Entitys;
import org.niux.aims.service.IApplicationService;
import org.niux.aims.service.IEntityTypeService;
import org.niux.aims.service.IEntitysService;
import org.niux.aims.service.ILogsService;
import org.niux.aims.vo.AccountVO;
import org.niux.aims.vo.ApplicationVO;
import org.niux.aims.vo.EntityTypeVO;
import org.niux.aims.vo.EntityVO;

/**
 * 
 * Copyright (c) 2009,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：EditEntityAction.java<br>
 * 摘    要：实体数据编辑页面类<br>
 * <br>
 * 当前版本：<br>
 * 作    者：铭显 Email: <a href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：@since 2013-8-15<br>
 * <br>
 * 取代版本：<br>
 * 原作者  ：<br>
 * 完成日期：@since Oct 22, 2009<br>
 */
public class EditEntityAction extends AimsBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5785730890949735911L;
	private Log log = LogFactory.getLog(this.getClass());
	
	@Resource(name="applicationService")
	private IApplicationService applicationService;
	
	@Resource(name="entityTypeService")
	private IEntityTypeService entityTypeService;
	
	@Resource(name="entityService")
	private IEntitysService entityService;
	
	@Resource(name="logsService")
	private ILogsService logsService;
	
	private String id;
	private String action;
	private String app;
	private String entityName;
	private String entityType;
	private String entityCode;
	private String parent;
	
	/**
	 * 
	 * 方法概述：页面渲染
	 * 描述：页面渲染
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-8-2
	 */
	public String showPage(){
		logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】进入实体编辑界面");
		
		Entitys entity = null;
		
		if(id != null){
			entity = entityService.findById(Integer.parseInt(id));
			super.getRequest().setAttribute("entity", entity);
		}
		
		List<ApplicationVO> apps = applicationService.findAll();
		List<EntityType> entityTypes = entityTypeService.findAll();
		
		List<Entitys> allEntitys = entityService.findAll();
		
		List<EntityVO> entitys = new ArrayList<EntityVO>();
		
		if(entity != null) {
			for (Entitys entityVo : allEntitys) {
				if(entityVo.getEntitys() == null && entityVo.getApplication().getId() == entity.getApplication().getId()){
					EntityVO menu = transformToVo(entityVo);
					entitys.add(menu);
				}
			}
			
			for(EntityVO menu : entitys){
				List<EntityVO> pages = new ArrayList<EntityVO>();
				
				for (Entitys entityVo : allEntitys) {
					if(entityVo.getEntitys() != null && entityVo.getEntitys().getId() == Integer.parseInt(menu.getId())){
						EntityVO page = transformToVo(entityVo);
						pages.add(page);
					}
				}
				
				menu.setChild(pages);
			}
			
			for(EntityVO menu : entitys){
				if(!menu.getChild().isEmpty()){
					for(EntityVO page : menu.getChild()){
						List<EntityVO> buttons = new ArrayList<EntityVO>();
						
						for (Entitys entityVo : allEntitys) {
							if(entityVo.getEntitys() != null && entityVo.getEntitys().getId() == Integer.parseInt(page.getId())){
								EntityVO button = transformToVo(entityVo);
								buttons.add(button);
							}
						}
						
						page.setChild(buttons);
					}
				}
			}
		}
		
		super.getRequest().setAttribute("apps", apps);
		super.getRequest().setAttribute("entityTypes", entityTypes);
		super.getRequest().setAttribute("entitys", entitys);
		
		return SUCCESS;
	}
	
	/**
	 * 
	 * 方法概述：DO转换成VO
	 * 描述：DO转换成VO
	 * @param entity
	 * @return
	 * 返回类型：EntityVO
	 * 修改人：梁铭显
	 * 修改时间：2013-8-25
	 */
	public EntityVO transformToVo(Entitys entity){
		EntityVO entityVo = new EntityVO();
		entityVo.setId(Integer.toString(entity.getId()));
		entityVo.setEntityName(entity.getEntityName());
		entityVo.setEntityCode(entity.getEntityCode());
		entityVo.setParent(entity.getEntitys() == null ? null : Integer.toString(entity.getEntitys().getId()));
		
		ApplicationVO appVo = new ApplicationVO();
		appVo.setId(entity.getApplication().getId());
		appVo.setAppName(entity.getApplication().getAppName());
		
		entityVo.setAppVo(appVo);
		
		EntityTypeVO entityTypeVo = new EntityTypeVO();
		entityTypeVo.setId(entity.getEntityType().getId());
		entityTypeVo.setEntityTypeName(entity.getEntityType().getEntityType());
		
		entityVo.setEntityTypeVo(entityTypeVo);
		
		return entityVo;
	}
	
	/**
	 * 
	 * 方法概述：新增实体
	 * 描述：新增实体
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-8-18
	 */
	public String addEntity(){
		log.debug(app + "=====" + entityName + "=====" + entityCode + "=====" + parent);
		EntityVO entityVo = new EntityVO();
		entityVo.setApp(app);
		entityVo.setEntityName(entityName);
		entityVo.setEntityCode(entityCode);
		entityVo.setEntityType(entityType);
		
		if(Integer.parseInt(parent) == 0){
			entityVo.setParent(null);
		} else {
			entityVo.setParent(parent);
		}
		
		entityService.save(entityVo);
		
		logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】新增实体【"+entityName+"】");
		
		return SUCCESS;
	}
	
	/**
	 * 
	 * 方法概述：修改实体
	 * 描述：修改实体
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-8-20
	 */
	public String updateEntity(){
		log.debug(app + "=====" + entityName + "=====" + entityCode + "=====" + parent);
		EntityVO entityVo = new EntityVO();
		entityVo.setId(id);
		entityVo.setApp(app);
		entityVo.setEntityName(entityName);
		entityVo.setEntityCode(entityCode);
		entityVo.setEntityType(entityType);
		
		if(Integer.parseInt(parent) == 0){
			entityVo.setParent(null);
		} else {
			entityVo.setParent(parent);
		}
		
		entityService.update(entityVo);
		
		logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】修改实体【"+entityName+"】");
		
		action = "Update";
		
		return SUCCESS;
	}
	
	/**
	 * 
	 * 方法概述：删除实体
	 * 描述：删除实体
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-8-20
	 */
	public String del(){
		Entitys entity = entityService.findById(Integer.parseInt(id));
		logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】删除实体【"+entity.getEntityName()+"】");
		entityService.del(Integer.parseInt(id));
		return SUCCESS;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getApp() {
		return app;
	}

	public void setApp(String app) {
		this.app = app;
	}

	public String getEntityName() {
		return entityName;
	}

	public void setEntityName(String entityName) {
		this.entityName = entityName;
	}

	public String getEntityType() {
		return entityType;
	}

	public void setEntityType(String entityType) {
		this.entityType = entityType;
	}

	public String getEntityCode() {
		return entityCode;
	}

	public void setEntityCode(String entityCode) {
		this.entityCode = entityCode;
	}

	public String getParent() {
		return parent;
	}

	public void setParent(String parent) {
		this.parent = parent;
	}
	
}
